{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b1da463",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from IPython.core.interactiveshell import InteractiveShell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3f6f950f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 78.1 ms\n",
      "Wall time: 94.5 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "data = pd.read_csv('C://Users//10203//Desktop//AAA//000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day', inplace = True)\n",
    "data.sort_values(by = ['Day'],axis=0, ascending=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "69f9f0bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1366.58</td>\n",
       "      <td>1368.69</td>\n",
       "      <td>1407.52</td>\n",
       "      <td>1361.21</td>\n",
       "      <td>1406.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>1406.37</td>\n",
       "      <td>1407.83</td>\n",
       "      <td>1433.78</td>\n",
       "      <td>1398.32</td>\n",
       "      <td>1409.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1409.68</td>\n",
       "      <td>1406.04</td>\n",
       "      <td>1463.95</td>\n",
       "      <td>1400.25</td>\n",
       "      <td>1463.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>1463.94</td>\n",
       "      <td>1477.15</td>\n",
       "      <td>1522.83</td>\n",
       "      <td>1477.15</td>\n",
       "      <td>1516.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-10</th>\n",
       "      <td>1516.60</td>\n",
       "      <td>1531.71</td>\n",
       "      <td>1546.72</td>\n",
       "      <td>1506.40</td>\n",
       "      <td>1545.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-25</th>\n",
       "      <td>3612.49</td>\n",
       "      <td>3614.05</td>\n",
       "      <td>3635.26</td>\n",
       "      <td>3601.74</td>\n",
       "      <td>3627.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-28</th>\n",
       "      <td>3627.91</td>\n",
       "      <td>3635.77</td>\n",
       "      <td>3641.59</td>\n",
       "      <td>3533.78</td>\n",
       "      <td>3533.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-29</th>\n",
       "      <td>3533.78</td>\n",
       "      <td>3528.40</td>\n",
       "      <td>3564.17</td>\n",
       "      <td>3515.52</td>\n",
       "      <td>3563.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-30</th>\n",
       "      <td>3563.74</td>\n",
       "      <td>3566.73</td>\n",
       "      <td>3573.68</td>\n",
       "      <td>3538.11</td>\n",
       "      <td>3572.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3572.88</td>\n",
       "      <td>3570.47</td>\n",
       "      <td>3580.60</td>\n",
       "      <td>3538.35</td>\n",
       "      <td>3539.18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3871 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose     Open  Highest   Lowest    Close\n",
       "Day                                                     \n",
       "2000-01-04   1366.58  1368.69  1407.52  1361.21  1406.37\n",
       "2000-01-05   1406.37  1407.83  1433.78  1398.32  1409.68\n",
       "2000-01-06   1409.68  1406.04  1463.95  1400.25  1463.94\n",
       "2000-01-07   1463.94  1477.15  1522.83  1477.15  1516.60\n",
       "2000-01-10   1516.60  1531.71  1546.72  1506.40  1545.11\n",
       "...              ...      ...      ...      ...      ...\n",
       "2015-12-25   3612.49  3614.05  3635.26  3601.74  3627.91\n",
       "2015-12-28   3627.91  3635.77  3641.59  3533.78  3533.78\n",
       "2015-12-29   3533.78  3528.40  3564.17  3515.52  3563.74\n",
       "2015-12-30   3563.74  3566.73  3573.68  3538.11  3572.88\n",
       "2015-12-31   3572.88  3570.47  3580.60  3538.35  3539.18\n",
       "\n",
       "[3871 rows x 5 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['2000-01':'2015-12'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ddcd3f5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Numpy_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1366.58</td>\n",
       "      <td>1368.69</td>\n",
       "      <td>1407.52</td>\n",
       "      <td>1361.21</td>\n",
       "      <td>1406.37</td>\n",
       "      <td>0.029116</td>\n",
       "      <td>0.029116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>1406.37</td>\n",
       "      <td>1407.83</td>\n",
       "      <td>1433.78</td>\n",
       "      <td>1398.32</td>\n",
       "      <td>1409.68</td>\n",
       "      <td>0.002354</td>\n",
       "      <td>0.002354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1409.68</td>\n",
       "      <td>1406.04</td>\n",
       "      <td>1463.95</td>\n",
       "      <td>1400.25</td>\n",
       "      <td>1463.94</td>\n",
       "      <td>0.038491</td>\n",
       "      <td>0.038491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>1463.94</td>\n",
       "      <td>1477.15</td>\n",
       "      <td>1522.83</td>\n",
       "      <td>1477.15</td>\n",
       "      <td>1516.60</td>\n",
       "      <td>0.035971</td>\n",
       "      <td>0.035971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-10</th>\n",
       "      <td>1516.60</td>\n",
       "      <td>1531.71</td>\n",
       "      <td>1546.72</td>\n",
       "      <td>1506.40</td>\n",
       "      <td>1545.11</td>\n",
       "      <td>0.018799</td>\n",
       "      <td>0.018799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-25</th>\n",
       "      <td>3612.49</td>\n",
       "      <td>3614.05</td>\n",
       "      <td>3635.26</td>\n",
       "      <td>3601.74</td>\n",
       "      <td>3627.91</td>\n",
       "      <td>0.004269</td>\n",
       "      <td>0.004269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-28</th>\n",
       "      <td>3627.91</td>\n",
       "      <td>3635.77</td>\n",
       "      <td>3641.59</td>\n",
       "      <td>3533.78</td>\n",
       "      <td>3533.78</td>\n",
       "      <td>-0.025946</td>\n",
       "      <td>-0.025946</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-29</th>\n",
       "      <td>3533.78</td>\n",
       "      <td>3528.40</td>\n",
       "      <td>3564.17</td>\n",
       "      <td>3515.52</td>\n",
       "      <td>3563.74</td>\n",
       "      <td>0.008478</td>\n",
       "      <td>0.008478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-30</th>\n",
       "      <td>3563.74</td>\n",
       "      <td>3566.73</td>\n",
       "      <td>3573.68</td>\n",
       "      <td>3538.11</td>\n",
       "      <td>3572.88</td>\n",
       "      <td>0.002565</td>\n",
       "      <td>0.002565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3572.88</td>\n",
       "      <td>3570.47</td>\n",
       "      <td>3580.60</td>\n",
       "      <td>3538.35</td>\n",
       "      <td>3539.18</td>\n",
       "      <td>-0.009432</td>\n",
       "      <td>-0.009432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3871 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose     Open  Highest   Lowest    Close  Apply_return  \\\n",
       "Day                                                                      \n",
       "2000-01-04   1366.58  1368.69  1407.52  1361.21  1406.37      0.029116   \n",
       "2000-01-05   1406.37  1407.83  1433.78  1398.32  1409.68      0.002354   \n",
       "2000-01-06   1409.68  1406.04  1463.95  1400.25  1463.94      0.038491   \n",
       "2000-01-07   1463.94  1477.15  1522.83  1477.15  1516.60      0.035971   \n",
       "2000-01-10   1516.60  1531.71  1546.72  1506.40  1545.11      0.018799   \n",
       "...              ...      ...      ...      ...      ...           ...   \n",
       "2015-12-25   3612.49  3614.05  3635.26  3601.74  3627.91      0.004269   \n",
       "2015-12-28   3627.91  3635.77  3641.59  3533.78  3533.78     -0.025946   \n",
       "2015-12-29   3533.78  3528.40  3564.17  3515.52  3563.74      0.008478   \n",
       "2015-12-30   3563.74  3566.73  3573.68  3538.11  3572.88      0.002565   \n",
       "2015-12-31   3572.88  3570.47  3580.60  3538.35  3539.18     -0.009432   \n",
       "\n",
       "            Numpy_return  \n",
       "Day                       \n",
       "2000-01-04      0.029116  \n",
       "2000-01-05      0.002354  \n",
       "2000-01-06      0.038491  \n",
       "2000-01-07      0.035971  \n",
       "2000-01-10      0.018799  \n",
       "...                  ...  \n",
       "2015-12-25      0.004269  \n",
       "2015-12-28     -0.025946  \n",
       "2015-12-29      0.008478  \n",
       "2015-12-30      0.002565  \n",
       "2015-12-31     -0.009432  \n",
       "\n",
       "[3871 rows x 7 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Numpy_return'] = (data_new['Close'].values / data_new['Preclose'].values) - 1\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "effa2450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-31</th>\n",
       "      <td>1535.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-02-29</th>\n",
       "      <td>1714.58</td>\n",
       "      <td>1535.00</td>\n",
       "      <td>0.116990</td>\n",
       "      <td>0.110638</td>\n",
       "      <td>2000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-03-31</th>\n",
       "      <td>1800.22</td>\n",
       "      <td>1714.58</td>\n",
       "      <td>0.049948</td>\n",
       "      <td>0.048741</td>\n",
       "      <td>2000</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-04-30</th>\n",
       "      <td>1836.32</td>\n",
       "      <td>1800.22</td>\n",
       "      <td>0.020053</td>\n",
       "      <td>0.019855</td>\n",
       "      <td>2000</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-05-31</th>\n",
       "      <td>1894.55</td>\n",
       "      <td>1836.32</td>\n",
       "      <td>0.031710</td>\n",
       "      <td>0.031218</td>\n",
       "      <td>2000</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose  Raw_return  Log_return  Year  Month\n",
       "Day                                                               \n",
       "2000-01-31  1535.00       NaN         NaN         NaN  2000      1\n",
       "2000-02-29  1714.58   1535.00    0.116990    0.110638  2000      2\n",
       "2000-03-31  1800.22   1714.58    0.049948    0.048741  2000      3\n",
       "2000-04-30  1836.32   1800.22    0.020053    0.019855  2000      4\n",
       "2000-05-31  1894.55   1836.32    0.031710    0.031218  2000      5"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data = data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data['Preclose'] = Month_data2['Close'].shift(1)\n",
    "Month_data['Raw_return'] = Month_data2['Close'] / Month_data2['Preclose'] - 1\n",
    "Month_data['Log_return'] = np.log(Month_data2['Close']) - np.log(Month_data2['Preclose'])\n",
    "Month_data['Year'] = Month_data2.index.year\n",
    "Month_data['Month'] = Month_data2.index.month\n",
    "Month_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e00153f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-03-31</th>\n",
       "      <td>1800.22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-06-30</th>\n",
       "      <td>1928.11</td>\n",
       "      <td>1800.22</td>\n",
       "      <td>0.071041</td>\n",
       "      <td>0.068631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-09-30</th>\n",
       "      <td>1910.16</td>\n",
       "      <td>1928.11</td>\n",
       "      <td>-0.009310</td>\n",
       "      <td>-0.009353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.48</td>\n",
       "      <td>1910.16</td>\n",
       "      <td>0.085501</td>\n",
       "      <td>0.082041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-03-31</th>\n",
       "      <td>2112.78</td>\n",
       "      <td>2073.48</td>\n",
       "      <td>0.018954</td>\n",
       "      <td>0.018776</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose  Raw_return  Log_return\n",
       "Day                                                  \n",
       "2000-03-31  1800.22       NaN         NaN         NaN\n",
       "2000-06-30  1928.11   1800.22    0.071041    0.068631\n",
       "2000-09-30  1910.16   1928.11   -0.009310   -0.009353\n",
       "2000-12-31  2073.48   1910.16    0.085501    0.082041\n",
       "2001-03-31  2112.78   2073.48    0.018954    0.018776"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Quarter_data = data_new.resample('QE')['Close'].last().to_frame()\n",
    "Quarter_data['Preclose'] = Quarter_data2['Close'].shift(1)\n",
    "Quarter_data['Raw_return'] = Quarter_data2['Close'] / Quarter_data2['Preclose'] - 1\n",
    "Quarter_data['Log_return'] = np.log(Quarter_data2['Close']) - np.log(Quarter_data2['Preclose'])\n",
    "print\n",
    "Quarter_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7afc7c50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.48</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.97</td>\n",
       "      <td>2073.48</td>\n",
       "      <td>-0.206180</td>\n",
       "      <td>-0.230898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.65</td>\n",
       "      <td>1645.97</td>\n",
       "      <td>-0.175167</td>\n",
       "      <td>-0.192575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.04</td>\n",
       "      <td>1357.65</td>\n",
       "      <td>0.102670</td>\n",
       "      <td>0.097735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.50</td>\n",
       "      <td>1497.04</td>\n",
       "      <td>-0.153997</td>\n",
       "      <td>-0.167233</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose  Raw_return  Log_return\n",
       "Day                                                  \n",
       "2000-12-31  2073.48       NaN         NaN         NaN\n",
       "2001-12-31  1645.97   2073.48   -0.206180   -0.230898\n",
       "2002-12-31  1357.65   1645.97   -0.175167   -0.192575\n",
       "2003-12-31  1497.04   1357.65    0.102670    0.097735\n",
       "2004-12-31  1266.50   1497.04   -0.153997   -0.167233"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data = data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data['Preclose'] = Year_data2['Close'].shift(1)\n",
    "Year_data['Raw_return'] = Year_data2['Close'] / Year_data2['Preclose'] - 1\n",
    "Year_data['Log_return'] = np.log(Year_data2['Close']) - np.log(Year_data2['Preclose'])\n",
    "print\n",
    "Year_data.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
